#!/usr/bin/env bash

# 在安装 Python 环境之前执行

# 设置 vendor 到 PATH
export PATH=$PATH:$ROOT_DIR/vendor/

# 为 heroku pip-install 提供可编辑目录
mkdir -p /app/.heroku/src

# 代替 runtime-fixer 来移除 runtime.txt 的空白
if [ -f runtime.txt ]; then
    sed -i '/^$/d' runtime.txt
fi

# 移除不需要的步骤
echo "#!/usr/bin/env bash" > "${BIN_DIR}/steps/mercurial"
echo "#!/usr/bin/env bash" > "${BIN_DIR}/steps/pylibmc"
echo "#!/usr/bin/env bash" > "${BIN_DIR}/steps/cryptography"
echo "#!/usr/bin/env bash" > "${BIN_DIR}/steps/geo-libs"
echo "#!/usr/bin/env bash" > "${BIN_DIR}/steps/gdal"

# 对目录 /var/cache/apt/archives/partial/ 无权限会安装失败
echo "#!/usr/bin/env bash" > "${BIN_DIR}/steps/sqlite3"
buildpack_sqlite3_install(){
    mkdir -p "${CACHE_DIR}/.heroku"
    echo "" > "${CACHE_DIR}/.heroku/python-sqlite3-version"
}

mkdir -p ~/.pip/
echo "[global]" > ~/.pip/pip.conf

# 设置 pip 首选源
if [ -n "${PIP_INDEX_URL}" ]; then
    echo "index-url = ${PIP_INDEX_URL}" >> ~/.pip/pip.conf
fi

# 设置 pip 次选源
if [ -n "${PIP_EXTRA_INDEX_URL}" ]; then
    echo "extra-index-url = ${PIP_EXTRA_INDEX_URL}" >> ~/.pip/pip.conf
fi

# 设置 pip 信任域名
if [ -n "${PIP_INDEX_HOST}" ]; then
    echo "trusted-host = ${PIP_INDEX_HOST}" >> ~/.pip/pip.conf
fi

# 指定 pip 版本
if [ -n "${PIP_VERSION}" ]; then
    export PIP_UPDATE=${PIP_VERSION}
    # 设置这个的话 get-pip 会报错
    unset PIP_VERSION
fi